home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / compress / gnucpio.zip / COPYOUT.C < prev    next >
C/C++ Source or Header  |  1996-01-01  |  23KB  |  811 lines

  1. /* copyout.c - create a cpio archive
  2.    Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include "filetypes.h"
  22. #include "system.h"
  23. #include "cpiohdr.h"
  24. #include "dstring.h"
  25. #include "extern.h"
  26. #include "defer.h"
  27. #include "rmt.h"
  28.  
  29. static unsigned long read_for_checksum ();
  30. static void tape_clear_rest_of_block ();
  31. static void tape_pad_output ();
  32. static int last_link ();
  33. static int count_defered_links_to_dev_ino ();
  34. static void add_link_defer ();
  35. static void writeout_other_defers ();
  36. static void writeout_final_defers();
  37. static void writeout_defered_file ();
  38.  
  39. /* Write out header FILE_HDR, including the file name, to file
  40.    descriptor OUT_DES.  */
  41.  
  42. void
  43. write_out_header (file_hdr, out_des)
  44.      struct new_cpio_header *file_hdr;
  45.      int out_des;
  46. {
  47.   if (archive_format == arf_newascii || archive_format == arf_crcascii)
  48.     {
  49.       char ascii_header[112];
  50.       char *magic_string;
  51.  
  52.       if (archive_format == arf_crcascii)
  53.     magic_string = "070702";
  54.       else
  55.     magic_string = "070701";
  56.       sprintf (ascii_header,
  57.            "%6s%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx",
  58.            magic_string,
  59.            file_hdr->c_ino, file_hdr->c_mode, file_hdr->c_uid,
  60.            file_hdr->c_gid, file_hdr->c_nlink, file_hdr->c_mtime,
  61.          file_hdr->c_filesize, file_hdr->c_dev_maj, file_hdr->c_dev_min,
  62.        file_hdr->c_rdev_maj, file_hdr->c_rdev_min, file_hdr->c_namesize,
  63.            file_hdr->c_chksum);
  64.       tape_buffered_write (ascii_header, out_des, 110L);
  65.  
  66.       /* Write file name to output.  */
  67.       tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
  68.       tape_pad_output (out_des, file_hdr->c_namesize + 110);
  69.     }
  70.   else if (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
  71.     {
  72.       char ascii_header[78];
  73. #ifndef __MSDOS__
  74.       dev_t dev;
  75.       dev_t rdev;
  76.  
  77.       if (archive_format == arf_oldascii)
  78.     {
  79.       dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
  80.       rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
  81.     }
  82.       else
  83.     {
  84.       /* HP/UX cpio creates archives that look just like ordinary archives,
  85.          but for devices it sets major = 0, minor = 1, and puts the
  86.          actual major/minor number in the filesize field.  */
  87.       switch (file_hdr->c_mode & CP_IFMT)
  88.         {
  89.           case CP_IFCHR:
  90.           case CP_IFBLK:
  91. #ifdef CP_IFSOCK
  92.           case CP_IFSOCK:
  93. #endif
  94. #ifdef CP_IFIFO
  95.           case CP_IFIFO:
  96. #endif
  97.         file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
  98.                         file_hdr->c_rdev_min);
  99.         rdev = 1;
  100.         break;
  101.           default:
  102.         dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
  103.         rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
  104.         break;
  105.         }
  106.     }
  107. #else
  108.       int dev = 0, rdev = 0;
  109. #endif
  110.  
  111. #ifndef __EMX__
  112.       if ((file_hdr->c_ino >> 16) != 0)
  113.     error (0, 0, "%s: truncating inode number", file_hdr->c_name);
  114. #endif
  115.  
  116.       sprintf (ascii_header,
  117.            "%06o%06o%06lo%06lo%06lo%06lo%06lo%06o%011lo%06lo%011lo",
  118.            file_hdr->c_magic & 0xFFFF, dev & 0xFFFF,
  119.            file_hdr->c_ino & 0xFFFF, file_hdr->c_mode & 0xFFFF,
  120.            file_hdr->c_uid & 0xFFFF, file_hdr->c_gid & 0xFFFF,
  121.            file_hdr->c_nlink & 0xFFFF, rdev & 0xFFFF,
  122.            file_hdr->c_mtime, file_hdr->c_namesize & 0xFFFF,
  123.            file_hdr->c_filesize);
  124.       tape_buffered_write (ascii_header, out_des, 76L);
  125.  
  126.       /* Write file name to output.  */
  127.       tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
  128.     }
  129.   else if (archive_format == arf_tar || archive_format == arf_ustar)
  130.     {
  131.       write_out_tar_header (file_hdr, out_des);
  132.     }
  133.   else
  134.     {
  135.       struct old_cpio_header short_hdr;
  136.  
  137.       short_hdr.c_magic = 070707;
  138.       short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
  139.  
  140. #ifndef __EMX__
  141.       if ((file_hdr->c_ino >> 16) != 0)
  142.     error (0, 0, "%s: truncating inode number", file_hdr->c_name);
  143. #endif
  144.  
  145.       short_hdr.c_ino = file_hdr->c_ino & 0xFFFF;
  146.       short_hdr.c_mode = file_hdr->c_mode & 0xFFFF;
  147.       short_hdr.c_uid = file_hdr->c_uid & 0xFFFF;
  148.       short_hdr.c_gid = file_hdr->c_gid & 0xFFFF;
  149.       short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF;
  150.       if (archive_format != arf_hpbinary)
  151.     short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
  152.       else
  153.     {
  154.       switch (file_hdr->c_mode & CP_IFMT)
  155.         {
  156.           /* HP/UX cpio creates archives that look just like ordinary 
  157.          archives, but for devices it sets major = 0, minor = 1, and 
  158.          puts the actual major/minor number in the filesize field.  */
  159.           case CP_IFCHR:
  160. #ifdef CP_IFBLK
  161.           case CP_IFBLK:
  162. #endif
  163. #ifdef CP_IFSOCK
  164.           case CP_IFSOCK:
  165. #endif
  166. #ifdef CP_IFIFO
  167.           case CP_IFIFO:
  168. #endif
  169.         file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
  170.                         file_hdr->c_rdev_min);
  171.         short_hdr.c_rdev = makedev (0, 1);
  172.         break;
  173.           default:
  174.         short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, 
  175.                         file_hdr->c_rdev_min);
  176.         break;
  177.         }
  178.     }
  179.       short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16;
  180.       short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF;
  181.  
  182.       short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
  183.  
  184.       short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16;
  185.       short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF;
  186.  
  187.       /* Output the file header.  */
  188.       tape_buffered_write ((char *) &short_hdr, out_des, 26L);
  189.  
  190.       /* Write file name to output.  */
  191.       tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
  192.  
  193.       tape_pad_output (out_des, file_hdr->c_namesize + 26);
  194.     }
  195. }
  196.  
  197. /* Read a list of file names from the standard input
  198.    and write a cpio collection on the standard output.
  199.    The format of the header depends on the compatibility (-c) flag.  */
  200.  
  201. void
  202. process_copy_out ()
  203. {
  204.   int res;            /* Result of functions.  */
  205.   dynamic_string input_name;    /* Name of file read from stdin.  */
  206.   struct utimbuf times;        /* For resetting file times after copy.  */
  207.   struct stat file_stat;    /* Stat record for file.  */
  208.   struct new_cpio_header file_hdr; /* Output header information.  */
  209.   int in_file_des;        /* Source file descriptor.  */
  210.   int out_file_des;        /* Output file descriptor.  */
  211.   char *p;
  212.  
  213.   /* Initialize the copy out.  */
  214.   ds_init (&input_name, 128);
  215.   /* Initialize this in case it has members we don't know to set.  */
  216.   bzero (×, sizeof (struct utimbuf));
  217.   file_hdr.c_magic = 070707;
  218.  
  219. #ifdef __MSDOS__
  220.   setmode (archive_des, O_BINARY);
  221. #endif
  222.   /* Check whether the output file might be a tape.  */
  223.   out_file_des = archive_des;
  224.   if (_isrmt (out_file_des))
  225.     {
  226.       output_is_special = 1;
  227.       output_is_seekable = 0;
  228.     }
  229.   else
  230.     {
  231.       if (fstat (out_file_des, &file_stat))
  232.     error (1, errno, "standard output is closed");
  233.       output_is_special =
  234. #ifdef S_ISBLK
  235.     S_ISBLK (file_stat.st_mode) ||
  236. #endif
  237.     S_ISCHR (file_stat.st_mode);
  238.       output_is_seekable = S_ISREG (file_stat.st_mode);
  239.     }
  240.  
  241.   if (append_flag)
  242.     {
  243.       process_copy_in ();
  244.       prepare_append (out_file_des);
  245.     }
  246.  
  247.   /* Copy files with names read from stdin.  */
  248.   while (ds_fgetstr (stdin, &input_name, name_end) != NULL)
  249.     {
  250.       /* Check for blank line.  */
  251.       if (input_name.ds_string[0] == 0)
  252.     {
  253.       error (0, 0, "blank line ignored");
  254.       continue;
  255.     }
  256.  
  257.       /* Process next file.  */
  258.       if ((*xstat) (input_name.ds_string, &file_stat) < 0)
  259.     error (0, errno, "%s", input_name.ds_string);
  260.       else
  261.     {
  262.       /* Set values in output header.  */
  263.       file_hdr.c_dev_maj = major (file_stat.st_dev);
  264.       file_hdr.c_dev_min = minor (file_stat.st_dev);
  265.       file_hdr.c_ino = file_stat.st_ino;
  266.       /* For POSIX systems that don't define the S_IF macros,
  267.          we can't assume that S_ISfoo means the standard Unix
  268.          S_IFfoo bit(s) are set.  So do it manually, with a
  269.          different name.  Bleah.  */
  270.       file_hdr.c_mode = (file_stat.st_mode & 07777);
  271.       if (S_ISREG (file_stat.st_mode))
  272.         file_hdr.c_mode |= CP_IFREG;
  273.       else if (S_ISDIR (file_stat.st_mode))
  274.         file_hdr.c_mode |= CP_IFDIR;
  275. #ifdef S_ISBLK
  276.       else if (S_ISBLK (file_stat.st_mode))
  277.         file_hdr.c_mode |= CP_IFBLK;
  278. #endif
  279. #ifdef S_ISCHR
  280.       else if (S_ISCHR (file_stat.st_mode))
  281.         file_hdr.c_mode |= CP_IFCHR;
  282. #endif
  283. #ifdef S_ISFIFO
  284.       else if (S_ISFIFO (file_stat.st_mode))
  285.         file_hdr.c_mode |= CP_IFIFO;
  286. #endif
  287. #ifdef S_ISLNK
  288.       else if (S_ISLNK (file_stat.st_mode))
  289.         file_hdr.c_mode |= CP_IFLNK;
  290. #endif
  291. #ifdef S_ISSOCK
  292.       else if (S_ISSOCK (file_stat.st_mode))
  293.         file_hdr.c_mode |= CP_IFSOCK;
  294. #endif
  295. #ifdef S_ISNWK
  296.       else if (S_ISNWK (file_stat.st_mode))
  297.         file_hdr.c_mode |= CP_IFNWK;
  298. #endif
  299.       file_hdr.c_uid = file_stat.st_uid;
  300.       file_hdr.c_gid = file_stat.st_gid;
  301.       file_hdr.c_nlink = file_stat.st_nlink;
  302.       file_hdr.c_rdev_maj = major (file_stat.st_rdev);
  303.       file_hdr.c_rdev_min = minor (file_stat.st_rdev);
  304.       file_hdr.c_mtime = file_stat.st_mtime;
  305.       file_hdr.c_filesize = file_stat.st_size;
  306.       file_hdr.c_chksum = 0;
  307.       file_hdr.c_tar_linkname = NULL;
  308.  
  309.       /* Strip leading `./' from the filename.  */
  310.       p = input_name.ds_string;
  311.       while (*p == '.' && *(p + 1) == '/')
  312.         {
  313.           ++p;
  314.           while (*p == '/')
  315.         ++p;
  316.         }
  317. #ifndef HPUX_CDF
  318.       file_hdr.c_name = p;
  319.       file_hdr.c_namesize = strlen (p) + 1;
  320. #else
  321.       if ( (archive_format != arf_tar) && (archive_format != arf_ustar) )
  322.         {
  323.           /* We mark CDF's in cpio files by adding a 2nd `/' after the
  324.          "hidden" directory name.  We need to do this so we can
  325.          properly recreate the directory as hidden (in case the
  326.          files of a directory go into the archive before the
  327.          directory itself (e.g from "find ... -depth ... | cpio")).  */
  328.           file_hdr.c_name = add_cdf_double_slashes (p);
  329.           file_hdr.c_namesize = strlen (file_hdr.c_name) + 1;
  330.         }
  331.       else
  332.         {
  333.           /* We don't mark CDF's in tar files.  We assume the "hidden"
  334.          directory will always go into the archive before any of
  335.          its files.  */
  336.           file_hdr.c_name = p;
  337.           file_hdr.c_namesize = strlen (p) + 1;
  338.         }
  339. #endif
  340.       if ((archive_format == arf_tar || archive_format == arf_ustar)
  341.           && is_tar_filename_too_long (file_hdr.c_name))
  342.         {
  343.           error (0, 0, "%s: file name too long", file_hdr.c_name);
  344.           continue;
  345.         }
  346.  
  347.       /* Copy the named file to the output.  */
  348.       switch (file_hdr.c_mode & CP_IFMT)
  349.         {
  350.         case CP_IFREG:
  351. #ifndef __MSDOS__
  352.           if (archive_format == arf_tar || archive_format == arf_ustar)
  353.         {
  354.           char *otherfile;
  355.           if ((otherfile = find_inode_file (file_hdr.c_ino,
  356.                             file_hdr.c_dev_maj,
  357.                             file_hdr.c_dev_min)))
  358.             {
  359.               file_hdr.c_tar_linkname = otherfile;
  360.               write_out_header (&file_hdr, out_file_des);
  361.               break;
  362.             }
  363.         }
  364.           if ( (archive_format == arf_newascii || archive_format == arf_crcascii)
  365.           && (file_hdr.c_nlink > 1) )
  366.         {
  367.           if (last_link (&file_hdr) )
  368.             {
  369.               writeout_other_defers (&file_hdr, out_file_des);
  370.             }
  371.           else
  372.             {
  373.               add_link_defer (&file_hdr);
  374.               break;
  375.             }
  376.         }
  377. #endif
  378.           in_file_des = open (input_name.ds_string,
  379.                   O_RDONLY | O_BINARY, 0);
  380.           if (in_file_des < 0)
  381.         {
  382.           error (0, errno, "%s", input_name.ds_string);
  383.           continue;
  384.         }
  385.  
  386.           if (archive_format == arf_crcascii)
  387.         file_hdr.c_chksum = read_for_checksum (in_file_des,
  388.                                file_hdr.c_filesize,
  389.                                input_name.ds_string);
  390.  
  391.           write_out_header (&file_hdr, out_file_des);
  392.           copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, input_name.ds_string);
  393.  
  394. #ifndef __MSDOS__
  395.           if (archive_format == arf_tar || archive_format == arf_ustar)
  396.         add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj,
  397.                file_hdr.c_dev_min);
  398. #endif
  399.  
  400.           tape_pad_output (out_file_des, file_hdr.c_filesize);
  401.  
  402.           if (close (in_file_des) < 0)
  403.         error (0, errno, "%s", input_name.ds_string);
  404.           if (reset_time_flag)
  405.         {
  406.           times.actime = file_stat.st_atime;
  407.           times.modtime = file_stat.st_mtime;
  408.           if (utime (file_hdr.c_name, ×) < 0)
  409.             error (0, errno, "%s", file_hdr.c_name);
  410.         }
  411.           break;
  412.  
  413.         case CP_IFDIR:
  414.           file_hdr.c_filesize = 0;
  415.           write_out_header (&file_hdr, out_file_des);
  416.           break;
  417.  
  418. #ifndef __MSDOS__
  419.         case CP_IFCHR:
  420.         case CP_IFBLK:
  421. #ifdef CP_IFSOCK
  422.         case CP_IFSOCK:
  423. #endif
  424. #ifdef CP_IFIFO
  425.         case CP_IFIFO:
  426. #endif
  427.           if (archive_format == arf_tar)
  428.         {
  429.           error (0, 0, "%s not dumped: not a regular file",
  430.              file_hdr.c_name);
  431.           continue;
  432.         }
  433.           else if (archive_format == arf_ustar)
  434.         {
  435.           char *otherfile;
  436.           if ((otherfile = find_inode_file (file_hdr.c_ino,
  437.                             file_hdr.c_dev_maj,
  438.                             file_hdr.c_dev_min)))
  439.             {
  440.               /* This file is linked to another file already in the 
  441.                  archive, so write it out as a hard link. */
  442.               file_hdr.c_mode = (file_stat.st_mode & 07777);
  443.               file_hdr.c_mode |= CP_IFREG;
  444.               file_hdr.c_tar_linkname = otherfile;
  445.               write_out_header (&file_hdr, out_file_des);
  446.               break;
  447.             }
  448.           add_inode (file_hdr.c_ino, file_hdr.c_name, 
  449.                  file_hdr.c_dev_maj, file_hdr.c_dev_min);
  450.         }
  451.           file_hdr.c_filesize = 0;
  452.           write_out_header (&file_hdr, out_file_des);
  453.           break;
  454. #endif
  455.  
  456. #ifdef CP_IFLNK
  457.         case CP_IFLNK:
  458.           {
  459.         char *link_name = (char *) xmalloc (file_stat.st_size + 1);
  460.  
  461.         if (readlink (input_name.ds_string, link_name,
  462.                   file_stat.st_size) < 0)
  463.           {
  464.             error (0, errno, "%s", input_name.ds_string);
  465.             free (link_name);
  466.             continue;
  467.           }
  468.         if (archive_format == arf_tar || archive_format == arf_ustar)
  469.           {
  470.             if (file_stat.st_size + 1 > 100)
  471.               {
  472.             error (0, 0, "%s: symbolic link too long",
  473.                    file_hdr.c_name);
  474.               }
  475.             else
  476.               {
  477.             link_name[file_stat.st_size] = '\0';
  478.             file_hdr.c_tar_linkname = link_name;
  479.             write_out_header (&file_hdr, out_file_des);
  480.               }
  481.           }
  482.         else
  483.           {
  484.             write_out_header (&file_hdr, out_file_des);
  485.             tape_buffered_write (link_name, out_file_des, file_stat.st_size);
  486.             tape_pad_output (out_file_des, file_hdr.c_filesize);
  487.           }
  488.         free (link_name);
  489.           }
  490.           break;
  491. #endif
  492.  
  493.         default:
  494.           error (0, 0, "%s: unknown file type", input_name.ds_string);
  495.         }
  496.  
  497.       if (verbose_flag)
  498.         fprintf (stderr, "%s\n", input_name.ds_string);
  499.       if (dot_flag)
  500.         fputc ('.', stderr);
  501.     }
  502.     }
  503.  
  504.   writeout_final_defers(out_file_des);
  505.   /* The collection is complete; append the trailer.  */
  506.   file_hdr.c_ino = 0;
  507.   file_hdr.c_mode = 0;
  508.   file_hdr.c_uid = 0;
  509.   file_hdr.c_gid = 0;
  510.   file_hdr.c_nlink = 1;        /* Must be 1 for crc format.  */
  511.   file_hdr.c_dev_maj = 0;
  512.   file_hdr.c_dev_min = 0;
  513.   file_hdr.c_rdev_maj = 0;
  514.   file_hdr.c_rdev_min = 0;
  515.   file_hdr.c_mtime = 0;
  516.   file_hdr.c_chksum = 0;
  517.  
  518.   file_hdr.c_filesize = 0;
  519.   file_hdr.c_namesize = 11;
  520.   file_hdr.c_name = "TRAILER!!!";
  521.   if (archive_format != arf_tar && archive_format != arf_ustar)
  522.     write_out_header (&file_hdr, out_file_des);
  523.   else
  524.     {
  525.       tape_buffered_write (zeros_512, out_file_des, 512);
  526.       tape_buffered_write (zeros_512, out_file_des, 512);
  527.     }
  528.  
  529.   /* Fill up the output block.  */
  530.   tape_clear_rest_of_block (out_file_des);
  531.   tape_empty_output_buffer (out_file_des);
  532.   if (dot_flag)
  533.     fputc ('\n', stderr);
  534.   if (!quiet_flag)
  535.     {
  536.       res = (output_bytes + io_block_size - 1) / io_block_size;
  537.       if (res == 1)
  538.     fprintf (stderr, "1 block\n");
  539.       else
  540.     fprintf (stderr, "%d blocks\n", res);
  541.     }
  542. }
  543.  
  544. /* Read FILE_SIZE bytes of FILE_NAME from IN_FILE_DES and
  545.    compute and return a checksum for them.  */
  546.  
  547. static unsigned long
  548. read_for_checksum (in_file_des, file_size, file_name)
  549.      int in_file_des;
  550.      int file_size;
  551.      char *file_name;
  552. {
  553.   unsigned long crc;
  554.   char buf[BUFSIZ];
  555.   int bytes_left;
  556.   int bytes_read;
  557.   int i;
  558.  
  559.   crc = 0;
  560.  
  561.   for (bytes_left = file_size; bytes_left > 0; bytes_left -= bytes_read)
  562.     {
  563.       bytes_read = read (in_file_des, buf, BUFSIZ);
  564.       if (bytes_read < 0)
  565.     error (1, errno, "cannot read checksum for %s", file_name);
  566.       if (bytes_read == 0)
  567.     break;
  568.       for (i = 0; i < bytes_read; ++i)
  569.     crc += buf[i] & 0xff;
  570.     }
  571.   if (lseek (in_file_des, 0L, SEEK_SET))
  572.     error (1, errno, "cannot read checksum for %s", file_name);
  573.  
  574.   return crc;
  575. }
  576.  
  577. /* Write out NULs to fill out the rest of the current block on
  578.    OUT_FILE_DES.  */
  579.  
  580. static void
  581. tape_clear_rest_of_block (out_file_des)
  582.      int out_file_des;
  583. {
  584.   while (output_size < io_block_size)
  585.     {
  586.       if ((io_block_size - output_size) > 512)
  587.     tape_buffered_write (zeros_512, out_file_des, 512);
  588.       else
  589.     tape_buffered_write (zeros_512, out_file_des, io_block_size - output_size);
  590.     }
  591. }
  592.  
  593. /* Write NULs on OUT_FILE_DES to move from OFFSET (the current location)
  594.    to the end of the header.  */
  595.  
  596. static void
  597. tape_pad_output (out_file_des, offset)
  598.      int out_file_des;
  599.      int offset;
  600. {
  601.   int pad;
  602.  
  603.   if (archive_format == arf_newascii || archive_format == arf_crcascii)
  604.     pad = (4 - (offset % 4)) % 4;
  605.   else if (archive_format == arf_tar || archive_format == arf_ustar)
  606.     pad = (512 - (offset % 512)) % 512;
  607.   else if (archive_format != arf_oldascii && archive_format != arf_hpoldascii)
  608.     pad = (2 - (offset % 2)) % 2;
  609.   else
  610.     pad = 0;
  611.  
  612.   if (pad != 0)
  613.     tape_buffered_write (zeros_512, out_file_des, pad);
  614. }
  615.  
  616.  
  617. /* When creating newc and crc archives if a file has multiple (hard)
  618.    links, we don't put any of them into the archive until we have seen
  619.    all of them (or until we get to the end of the list of files that
  620.    are going into the archive and know that we have seen all of the links
  621.    to the file that we will see).  We keep these "defered" files on
  622.    this list.   */
  623.  
  624. struct deferment *deferouts = NULL;
  625.  
  626.  
  627. /* Is this file_hdr the last (hard) link to a file?  I.e., have
  628.    we already seen and defered all of the other links?  */
  629.  
  630. static int
  631. last_link (file_hdr)
  632.   struct new_cpio_header *file_hdr;
  633. {
  634.   int    other_files_sofar;
  635.  
  636.   other_files_sofar = count_defered_links_to_dev_ino (file_hdr);
  637.   if (file_hdr->c_nlink == (other_files_sofar + 1) )
  638.     {
  639.       return 1;
  640.     }
  641.   return 0;
  642. }
  643.  
  644. /* Count the number of other (hard) links to this file that have
  645.    already been defered.  */
  646.  
  647. static int
  648. count_defered_links_to_dev_ino (file_hdr)
  649.   struct new_cpio_header *file_hdr;
  650. {
  651.   struct deferment *d;
  652.   int    ino;
  653.   int     maj;
  654.   int   min;
  655.   int     count;
  656.   ino = file_hdr->c_ino;
  657.   maj = file_hdr->c_dev_maj;
  658.   min = file_hdr->c_dev_min;
  659.   count = 0;
  660.   for (d = deferouts; d != NULL; d = d->next)
  661.     {
  662.       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
  663.       && (d->header.c_dev_min == min) )
  664.     ++count;
  665.     }
  666.   return count;
  667. }
  668.  
  669. /* Add the file header for a link that is being defered to the deferouts
  670.    list.  */
  671.  
  672. static void
  673. add_link_defer (file_hdr)
  674.   struct new_cpio_header *file_hdr;
  675. {
  676.   struct deferment *d;
  677.   d = create_deferment (file_hdr);
  678.   d->next = deferouts;
  679.   deferouts = d;
  680. }
  681.  
  682. /* We are about to put a file into a newc or crc archive that is
  683.    multiply linked.  We have already seen and defered all of the
  684.    other links to the file but haven't written them into the archive.
  685.    Write the other links into the archive, and remove them from the
  686.    deferouts list.  */
  687.  
  688. static void
  689. writeout_other_defers (file_hdr, out_des)
  690.   struct new_cpio_header *file_hdr;
  691.   int out_des;
  692. {
  693.   struct deferment *d;
  694.   struct deferment *d_prev;
  695.   int    ino;
  696.   int     maj;
  697.   int   min;
  698.   ino = file_hdr->c_ino;
  699.   maj = file_hdr->c_dev_maj;
  700.   min = file_hdr->c_dev_min;
  701.   d_prev = NULL;
  702.   d = deferouts;
  703.   while (d != NULL)
  704.     {
  705.       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
  706.       && (d->header.c_dev_min == min) )
  707.     {
  708.       struct deferment *d_free;
  709.       d->header.c_filesize = 0;
  710.       write_out_header (&d->header, out_des);
  711.       if (d_prev != NULL)
  712.         d_prev->next = d->next;
  713.       else
  714.         deferouts = d->next;
  715.       d_free = d;
  716.       d = d->next;
  717.       free_deferment (d_free);
  718.     }
  719.       else
  720.     {
  721.       d_prev = d;
  722.       d = d->next;
  723.     }
  724.     }
  725.   return;
  726. }
  727. /* When writing newc and crc format archives we defer multiply linked
  728.    files until we have seen all of the links to the file.  If a file
  729.    has links to it that aren't going into the archive, then we will
  730.    never see the "last" link to the file, so at the end we just write 
  731.    all of the leftover defered files into the archive.  */
  732.  
  733. static void
  734. writeout_final_defers(out_des)
  735.   int    out_des;
  736. {
  737.   struct deferment *d;
  738.   int other_count;
  739.   while (deferouts != NULL)
  740.     {
  741.       d = deferouts;
  742.       other_count = count_defered_links_to_dev_ino (&d->header);
  743.       if (other_count == 1)
  744.     {
  745.       writeout_defered_file (&d->header, out_des);
  746.     }
  747.       else
  748.     {
  749.       struct new_cpio_header file_hdr;
  750.       file_hdr = d->header;
  751.       file_hdr.c_filesize = 0;
  752.       write_out_header (&file_hdr, out_des);
  753.     }
  754.       deferouts = deferouts->next;
  755.     }
  756. }
  757.  
  758. /* Write a file into the archive.  This code is the same as
  759.    the code in process_copy_out(), but we need it here too
  760.    for writeout_final_defers() to call.  */
  761.  
  762. static void
  763. writeout_defered_file (header, out_file_des)
  764.   struct new_cpio_header *header;
  765.   int out_file_des;
  766. {
  767.   int in_file_des;
  768.   struct new_cpio_header file_hdr;
  769.   struct utimbuf times;        /* For setting file times.  */
  770.   /* Initialize this in case it has members we don't know to set.  */
  771.   bzero (×, sizeof (struct utimbuf));
  772.  
  773.   file_hdr = *header;
  774.  
  775.  
  776.   in_file_des = open (header->c_name,
  777.               O_RDONLY | O_BINARY, 0);
  778.   if (in_file_des < 0)
  779.     {
  780.       error (0, errno, "%s", header->c_name);
  781.       return;
  782.     }
  783.  
  784.   if (archive_format == arf_crcascii)
  785.     file_hdr.c_chksum = read_for_checksum (in_file_des,
  786.                        file_hdr.c_filesize,
  787.                        header->c_name);
  788.  
  789.   write_out_header (&file_hdr, out_file_des);
  790.   copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, header->c_name);
  791.  
  792. #ifndef __MSDOS__
  793.   if (archive_format == arf_tar || archive_format == arf_ustar)
  794.     add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj,
  795.            file_hdr.c_dev_min);
  796. #endif
  797.  
  798.   tape_pad_output (out_file_des, file_hdr.c_filesize);
  799.  
  800.   if (close (in_file_des) < 0)
  801.     error (0, errno, "%s", header->c_name);
  802.   if (reset_time_flag)
  803.     {
  804.       times.actime = file_hdr.c_mtime;
  805.       times.modtime = file_hdr.c_mtime;
  806.       if (utime (file_hdr.c_name, ×) < 0)
  807.     error (0, errno, "%s", file_hdr.c_name);
  808.     }
  809.   return;
  810. }
  811.